package com.bac.rentmap.model;

import java.util.HashSet;
import java.util.Set;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.bac.rentmap.ad.flat.LocalAddress;
import com.bac.rentmap.ad.flat.LocalAddressPrefix;
import com.google.appengine.api.datastore.GeoPt;

/**
 *
 * @author Ivan Holub
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Building {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent(defaultFetchGroup = "true")
    private GeoPt geoPt;

    @Persistent
    private Integer housenumber;

    @Persistent
    private Set<String> houseNumberPrefixes;

    @Persistent
    private String buildingPart;

    @Persistent
    private String streetPrefix;

    @Persistent
    private String street;

    @Persistent
    private String lowercaseStreet;

    @Persistent
    private Integer number;

    @Persistent
    private String city;

    @Persistent
    private Integer version;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getHousenumber() {
        return housenumber;
    }

    public void setHousenumber(Integer housenumber) {
        this.housenumber = housenumber;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
        lowercaseStreet = (street != null ? street.toLowerCase() : street);
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreetPrefix() {
        return streetPrefix;
    }

    public void setLocalAddress(LocalAddress localAddress) {
        LocalAddressPrefix localAddressPrefix = localAddress.getPrefix();
        if (localAddressPrefix != null)
            streetPrefix = localAddressPrefix.getName();
        setStreet(localAddress.getStreet());
        housenumber = localAddress.getBuilding();
        if (housenumber != null) {
            houseNumberPrefixes = buildHouseNumberPrefixes(housenumber);
        }
        buildingPart = localAddress.getBuildingPart();
    }

    private Set<String> buildHouseNumberPrefixes(Object houseNumber) {
        Set<String> res = new HashSet<String>();
        String source = houseNumber.toString();
        for (int i = 1; i <= source.length(); i++) {
            res.add(source.substring(0, i));
        }
        return res;
    }

    public GeoPt getGeoPt() {
        return geoPt;
    }

    public void setGeoPt(GeoPt geoPt) {
        this.geoPt = geoPt;
    }

    public LocalAddress getLocalAddress() {
        LocalAddressPrefix localAddressPrefix = null;
        if (streetPrefix != null) {
            localAddressPrefix = LocalAddressPrefix.valueOfName(streetPrefix);
        }
        return new LocalAddress(localAddressPrefix, street, housenumber,
                buildingPart);
    }

    @Override
    public String toString() {
        return "Building[" + getLocalAddress() + ", " + geoPt + "]";
    }

    Set<String> getHouseNumberPrefixes() {
        return houseNumberPrefixes;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

}
